<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path;
%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0">
<title>点餐-教师</title>
<jsp:include page="/WEB-INF/page/common/_header.jsp"></jsp:include>
<style type="text/css">
	.switchCheck{
		border-color: #04be02;
    	background-color: #04be02;
	}
	.switchCheck:after{
		background-color: #fff !important;
		content: " " !important;
		margin-left: 20px;
	}
	.switchCheck:before{
		background-color: #04be02 !important;
	}
	.switchCheck1:after{
		background-color: #fff !important;
		content: " " !important;
	}
	.switchCheck1:before{
		background-color: #04be02 !important;
	}
	
	.noswitchCheck{
		border-color: #fff;
    	background-color: #fff;
	}
	.noswitchCheck:checked{
		border-color: #dfdfdf !important;
    	background-color: #fff !important;
	}
	.noswitchCheck:after{
		background-color: #fff !important;
		content: " " !important;
		margin-left: -20px;
	}
	.noswitchCheck:before{
		background-color: #fff !important;
	}
	.noswitchCheck1:checked{
		border-color: #dfdfdf !important;
    	background-color: #fff !important;
	}
	.noswitchCheck1:after{
		background-color: #fff !important;
		content: " " !important;
		margin-left: 0px;
	}
	.noswitchCheck1:before{
		background-color: #fff !important;
	}
</style>
</head>
<body ontouchstart>
	<div class="weui_tab tab-bottom">
		<div class="weui-header bg-blue"> 
	  		<div class="weui-header-left"> <a onclick="back()" class="icon icon-109 f-white">返回</a> </div>
	    	<h1 class="weui-header-title">每日点餐</h1>
	    </div>
	    <div class="weui_cells_title" style="line-height: 25px;color: #000;">
        	1.<span class="f-red">为倡议习近平重要指示，强调坚决制止餐饮浪费行为切实培养节约习惯，在全社会营造浪费可耻节约为荣的氛围。</span><br>
        	2.<span class="f-red">当天下午6点之前预定明天的午餐、晚餐，过了下午6点就不能预订了</span><br>
        	3.<span class="f-red">预定成功后，下午6点以后就不能取消</span>
        </div>
		<div class="weui_tab_bd">
			<div class="weui_cells mt0">
			    <div class="weui_cell tz-history-title">
	                <div class="weui_cell_bd weui_cell_primary">
	                    <p class="bold pr">点餐人：${user.teacher.name }</p>
	                </div>
	            </div>
			</div>
			<div class="weui_cells mt0">
			    <div class="weui_cell lblue tz-history-title">
	                <div class="weui_cell_bd weui_cell_primary">
	                    <p class="bold pr"><span id="tomorrowDate"></span></p>
	                </div>
	            </div>
	            <div id="tomorrowOrder"></div>
			</div>
			
			<div class="weui_cells weui_cells_radio">
			    <div class="weui_cell lblue tz-history-title">
	                <div class="weui_cell_bd weui_cell_primary">
	                    <p class="bold pr"><span id="todayDate"></span></p>
	                </div>
	            </div>
	            <div id="todayOrder">
	            </div>
			</div>
		</div>
	</div>
	<jsp:include page="/WEB-INF/page/common/_footer.jsp"></jsp:include>
<script type="text/javascript">
	$(function() {
		$("#tomorrowDate").html(getMyDay(addDay(new Date(),1))+"点餐预定("+getTime(addDay(new Date(),1),'yyyy-MM-dd')+")");
		$("#todayDate").html("今日我的预定("+getTime(new Date(),'yyyy-MM-dd')+")");
		queryTomorrowOrder();
		queryTodayOrder();
	})
	function queryTomorrowOrder() {
		var date = getTime(addDay(new Date(),1),'yyyy-MM-dd');
		$.ajax({
	   		url: basePath + "/ls/order/food/queryDateOrder",
	   		type: 'POST',
	   		dataType : "json",
	   		data: {
	   			orderDate: date
	   		},
	   		success: function(data) {
	   			var lunch,dinner;
	   			if(data != null && data.length > 0) {
	   				for(var i in data) {
	   					if(data[i].orderType == '午餐') lunch = data[i].id;
	   					else if(data[i].orderType == '晚餐') dinner = data[i].id;
	   				}
	   			}
	   			var html = '<div class="weui_cell weui_cell_switch">\
	                <div class="weui_cell_hd weui_cell_primary">午餐</div>\
	                <div class="weui_cell_ft">\
	                    <input class="weui_switch" type="checkbox" '+(lunch ? "checked data-id=\""+lunch+"\"" : "")+' onclick="sumitOrder(this,\'午餐\')">\
	                </div>\
	            </div>\
                <div class="weui_cell weui_cell_switch">\
	                <div class="weui_cell_hd weui_cell_primary">晚餐</div>\
	                <div class="weui_cell_ft">\
	                    <input class="weui_switch" type="checkbox" '+(dinner ? "checked data-id=\""+dinner+"\"" : '')+' onclick="sumitOrder(this,\'晚餐\')">\
	                </div>\
	            </div>';
	            $("#tomorrowOrder").html(html);
 	   		},
	   		error: function(data) {
	   		}
		});
	}
	function queryTodayOrder() {
		var date = getTime(new Date(),'yyyy-MM-dd');
		$.ajax({
	   		url: basePath + "/ls/order/food/queryDateOrder",
	   		type: 'POST',
	   		dataType : "json",
	   		data: {
	   			orderDate: date
	   		},
	   		success: function(data) {
	   			var html = '';
	   			if(data != null && data.length > 0) {
	   				for(var i in data) {
	   					html += '<label class="weui_cell weui_check_label">\
					                <div class="weui_cell_bd weui_cell_primary">\
			                    <p>'+data[i].orderType+'</p>\
			                </div>\
			                <div class="weui_cell_ft">\
			                	<input type="radio" class="weui_check" checked="checked">\
			                    <span class="weui_icon_checked"></span>\
			                </div>\
			            </label>';
	   				}
	   			}else {
	   				html = "<p class='pl10 pb10 f-red'>没有预约</p>";
	   			}
	   			$("#todayOrder").html(html);
 	   		},
	   		error: function(data) {
	   		}
		});
	}
	function sumitOrder(thiz, orderType) {
		var id = $(thiz).attr("data-id");
		if(id) {
			$(thiz).removeClass("switchCheck switchCheck1 noswitchCheck noswitchCheck1").addClass(thiz.checked ? "switchCheck1":"switchCheck");
			orderCancel(thiz, id);
		}else {
			$(thiz).removeClass("switchCheck switchCheck1 noswitchCheck noswitchCheck1").removeAttr("checked");
			orderSubmit(thiz, orderType);
		}
	}
	function orderSubmit(thiz,orderType) {
		var time = getTime(new Date(),'yyyy-MM-dd')+' 18:00:00';
		time = time.replace(/-/g,':').replace(' ',':');
		time = time.split(':');
		var limitDate = new Date(new Date(time[0],(time[1]-1),time[2],time[3],time[4],time[5])).getTime();
		var currDate = new Date().getTime();
		if(currDate > limitDate) {
			thiz.checked ? $(thiz).removeClass("noswitchCheck1").addClass("noswitchCheck") : $(thiz).removeClass("noswitchCheck").addClass("noswitchCheck1");
			$.toast("抱歉，超过了点餐时间！", "forbidden");
			return;
		}
		$.ajax({
	   		url: basePath + "/ls/order/food/orderSubmit",
	   		type: 'POST',
	   		dataType : "json",
	   		data: {
	   			orderType: orderType
	   		},
	   		success: function(data) {
	   			if(data.code == '000') {
	   				$(thiz).addClass(thiz.checked ? "switchCheck1":"switchCheck");
	   				$.toast("点餐成功");
	   				queryTomorrowOrder();
	   			}else {
	   				thiz.checked ? $(thiz).removeClass("noswitchCheck1").addClass("noswitchCheck") : $(thiz).removeClass("noswitchCheck").addClass("noswitchCheck1");
	   			 	$.toast(data.msg, "forbidden");
	   			}
 	   		},
	   		error: function(data) {
	   		}
		});
	}
	function orderCancel(thiz, id) {
		var time = getTime(new Date(),'yyyy-MM-dd')+' 18:00:00';
		time = time.replace(/-/g,':').replace(' ',':');
		time = time.split(':');
		var limitDate = new Date(new Date(time[0],(time[1]-1),time[2],time[3],time[4],time[5])).getTime();
		var currDate = new Date().getTime();
		if(currDate > limitDate) {
			thiz.checked ? $(thiz).removeClass("switchCheck").addClass("switchCheck1").removeAttr("checked") : $(thiz).removeClass("switchCheck1").addClass("switchCheck");
			$.toast("抱歉，超过了取消时间！", "forbidden");
			return;
		}
		 $.confirm("您确定要取消点餐吗?", "确认取消?", function() {
			 $.ajax({
		   		url: basePath + "/ls/order/food/orderCancel",
		   		type: 'POST',
		   		dataType : "json",
		   		data: {
		   			id: id
		   		},
		   		success: function(data) {
		   			if(data.code == '000') {
		   				$(thiz).removeClass("switchCheck switchCheck1").removeAttr("checked");
		   				$.toast("取消成功");
		   				queryTomorrowOrder();
		   			}else {
		   			 	$.toast(data.msg, "forbidden");
		   			}
	 	   		},
		   		error: function(data) {
		   		}
			});
         }, function() {
        	 thiz.checked ? $(thiz).removeClass("switchCheck").addClass("switchCheck1") : $(thiz).removeClass("switchCheck1").addClass("switchCheck");
         });
	}
	function getMyDay(date){
		var week;
		if(date.getDay()==0) week="周日"
		if(date.getDay()==1) week="周一"
		if(date.getDay()==2) week="周二"
		if(date.getDay()==3) week="周三"
		if(date.getDay()==4) week="周四"
		if(date.getDay()==5) week="周五"
		if(date.getDay()==6) week="周六"
		return week;
	}
</script>
</body>
</html>